package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Type;
import com.qf.service.TypeService;
import com.qf.service.TypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品分类
 * @author hanjiejie
 * @version V1.0
 * @Project java2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/5/25 0:38
 */
@WebServlet("/type")
public class TypeController extends BaseServlet {
    /**
     *查询所有商品分类信息
     * @param request
     * @param response
     * @return
     */
    public String findAllType(HttpServletRequest request, HttpServletResponse response){
  //调用业务层查询所有商品分类信息
        TypeService typeService = new TypeServiceImpl();
        List<Type> list = typeService.findAllType();
        //将数据放入到requst狱中保存 ，用来携带到页面中
        request.setAttribute("goodsTypeList", list);
        // 使用请求转发, 跳转到商品分类展示页面
        return "forward:/admin/showGoodsType.jsp";
    }

    /**
     * 商品下拉空需要添加商品的数据
     * @param request
     * @param response
     */
    public  void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
      //调用业务层查询所有商品分类信息
        TypeService typeService = new TypeServiceImpl();
        List<Type> list = typeService.findAllType();
        //将圣品分类集合数据转换成josn 格式 字符串
        String jsonString = JSON.toJSONString(list);
        //3. 通过流将json格式字符串写回到原页面中
        response.getWriter().println(jsonString);
    }
}
